<script>
    layout = eval(<%=@current_layout%>);
    titles = eval(<%=@article_titles%>);
    if (!layout) 
      var layout = new Array(); 
</script>
                   
 <hr />
 <a href="#" onclick="<%= " pars = 'layout='+layout.toJSONString() ; new Ajax.Updater('show_here', '/edit/set_issue_layout/" + session[:issue].id.to_s + "', {method:'post', parameters:pars,asynchronous:true, evalScripts:true}); return false;"-%>">
    <button> Change layout</button>
 </a>
|
 <a href="#" onclick="<%= "layout = []; pars = 'layout='+layout.toJSONString() ; new Ajax.Updater('show_here', '/edit/set_issue_layout/" + session[:issue].id.to_s + "', {method:'post', parameters:pars,asynchronous:true, evalScripts:true});  $('layout_reciever').innerHTML = ''; return false;"-%>">
     <button>Clear layout</button>
 </a>                    

<p>
Drag an article's title from the bottom box to the top one to make it appear on the front page of the issue <%= session[:issue].issue_number%>
</p>
<dl id="layout_reciever" style="border:1px black solid;padding: 10px">

</dl>

<div id="articles_list" style="border:1px black solid">
    <dd>
        <%=render :partial =>'sections_dragable_list' ,:collection => @sections%>
    </dd>
</div>


<script>

update_layout(layout, $('layout_reciever'))
 
</script>


<%= 
   drop_receiving_element "layout_reciever",
  #:update => "layout_reciever", 
  #:url => { :controller =>"edit", :action => "ajax"},
  :hoverclass => "hover_layout",
  :onDrop => " function (element) {  layout = addToLayout(element, layout); new Insertion.Bottom($('layout_reciever'), '<dd>'+element.innerHTML+'</dd>') }",
  :accept => "articles"
  #:loading => "",
  #  :complete => "$('layout_reciever').innerHTML = '1'" 
%>



 <%
 # FIXME: Find a way for the helper to accept the :parameters propertie, whe need to send the array in JSON to the server
 # mean while theres a version by hand it works but is pretty ugly
 link_to_remote("Change layout" ,
               :update => 'show_here',
               :url => { :action => :set_issue_layout , :id=>session[:issue].id},
               :completed=>"$(show_here).inner_html = document.write('aaa')",
               :method =>  'post',
               :before => "pars = 'layout'+layout.toJSONString()",
               :parameters => "pars")
                
  %>

 <div id="show_here" >
</div>